﻿#include "userdao.h"
#include <QDebug>
#include <QSqlError>

UserDao::UserDao(QSqlDatabase sqlDatabase)
{
    db = sqlDatabase;
}

bool UserDao::getList(QList<User> &list, const QVariantMap &param)
{
    bool res = false;
    User objectTmp;
    QSqlQuery sql_query(db);
    QString sqlStr = objectTmp.generateSelectSql();

    QStringList whereList;
    if (param.contains("username") && !param["username"].toString().isEmpty())
        whereList.append(QString("username like '%%1%'").arg(param["username"].toString()));
    if (param.contains("realName") && !param["realName"].toString().isEmpty())
        whereList.append(QString("realName like '%%1%'").arg(param["realName"].toString()));
    if (param.contains("remark") && !param["remark"].toString().isEmpty())
        whereList.append(QString("remark like '%%1%'").arg(param["remark"].toString()));
    if (param.contains("status") && !param["status"].toString().isEmpty())
        whereList.append(QString("status = %1").arg(param["status"].toInt()));
    if (param.contains("deptIds") && !param["deptIds"].toString().isEmpty())
        whereList.append(QString("deptId in (%1)").arg(param["deptIds"].toString()));

    if (param.contains("startTime") && !param["startTime"].toString().isEmpty())
        whereList.append(QString("createTime >= '%1'").arg(param["startTime"].toString()));
    if (param.contains("endTime") && !param["endTime"].toString().isEmpty())
        whereList.append(QString("createTime <= '%1'").arg(param["endTime"].toString()));

    QString whereSql = QString("%1%2").arg(whereList.size() > 0 ? " where " : "").arg(whereList.join(" and "));
    sqlStr.append(whereSql);

    qDebug() << sqlStr;

    if (sql_query.exec(sqlStr))
    {
        res = true;
        while (sql_query.next())
        {
            objectTmp.clear();
            objectTmp.fromSqlQuery(sql_query);
            objectTmp.setPassword("******"); //隐藏密码字段
            list.append(objectTmp);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::getTotal(uint64_t &total, const QVariantMap &param)
{
    bool res = false;
    User objectTmp;
    QSqlQuery sql_query(db);
    QString sqlStr = objectTmp.generateSelectCountSql();

    QStringList whereList;
    if (param.contains("userName") && !param["userName"].toString().isEmpty())
        whereList.append(QString("userName like '%%1%'").arg(param["userName"].toString()));
    if (param.contains("realName") && !param["realName"].toString().isEmpty())
        whereList.append(QString("realName like '%%1%'").arg(param["realName"].toString()));
    if (param.contains("remark") && !param["remark"].toString().isEmpty())
        whereList.append(QString("remark like '%%1%'").arg(param["remark"].toString()));
    if (param.contains("status") && !param["status"].toString().isEmpty())
        whereList.append(QString("status = %1").arg(param["status"].toInt()));
    if (param.contains("deptIds") && !param["deptIds"].toString().isEmpty())
        whereList.append(QString("deptId in (%1)").arg(param["deptIds"].toString()));

    if (param.contains("startTime") && !param["startTime"].toString().isEmpty())
        whereList.append(QString("createTime >= '%1'").arg(param["startTime"].toString()));
    if (param.contains("endTime") && !param["endTime"].toString().isEmpty())
        whereList.append(QString("createTime <= '%1'").arg(param["endTime"].toString()));

    QString whereSql = QString("%1%2").arg(whereList.size() > 0 ? " where " : "").arg(whereList.join(" and "));
    sqlStr.append(whereSql);

    qDebug() << sqlStr;

    if (sql_query.exec(sqlStr))
    {
        if (sql_query.next())
        {
            total = sql_query.value(0).toULongLong();
            res = true;
        }
        else
        {
            qCritical() << __FUNCTION__ << "Failed to get count!";
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::getOne(User &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateSelectSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
        if (sql_query.next())
        {
            object.fromSqlQuery(sql_query);
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::saveOne(User &object, QBaseEntity::PropertiesType propertiesType, QStringList properties)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateInsertSql();
    if (object.getId() != 0)
    {
        qDebug() << object.getPrimaryKey();
        sqlStr = object.generateUpdateSql(propertiesType, properties);
    }
    qDebug() << __FUNCTION__ << sqlStr;
    if (sql_query.exec(sqlStr))
    {
        res = true;
        //如果是修改直接跳出
        if (object.getId() != 0)
            return res;

        sqlStr = QString("%1 where username = '%2'").arg(object.generateSelectSql()).arg(object.getUsername());
        if (sql_query.exec(sqlStr))
        {
            if (sql_query.next())
            {
                object.setId(sql_query.value("id").toULongLong());
            }
            else
            {
                qCritical() << __FUNCTION__ << "not find by username!" << sqlStr;
            }
        }
        else
        {
            qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
        }
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}

bool UserDao::deleteOne(User &object)
{
    bool res = false;
    QSqlQuery sql_query(db);
    QString sqlStr = object.generateDeleteSql();
    qDebug() << object.generateDeleteSql();
    if (sql_query.exec(sqlStr))
    {
        res = true;
    }
    else
    {
        qCritical() << __FUNCTION__ << sql_query.lastError() << sqlStr;
    }
    return res;
}
